'use strict';

const Service = require('egg').Service;

class UserService extends Service {
  async getUserById() {
    try {
      const { ctx } = this;
      const token = ctx.request.header.authorization;
      const info = ctx.helper.analysisToken(token);
      const { data } = info;
      const user = await this.app.mysql.query('select * from users where id = ?', data);
      ctx.logger.info('获取用户信息', user[0]);
      const res = user[0];
      return ctx.helper.body.SUCCESS({ res, ctx, msg: '用户信息获取成功' });
    } catch (err) {
      const { ctx } = this;
      ctx.logger.error('获取用户信息', err);
      return ctx.helper.body.ERROR({ ctx, msg: '用户信息获取失败' });
    }
  }

  async login(data) {
    const { ctx, app } = this;
    const { username, password } = data;
    try {
      const user = await this.app.mysql.query('select id, username, gender, email, state, phone, avatar, country, city, password from users where username = ? ', username);
      ctx.logger.info('加密后密码', ctx.helper.aesEncrypt(password, app.config.jwt.secret));
      ctx.logger.info('解密后密码', ctx.helper.aesDecrypt(ctx.helper.aesEncrypt(password, app.config.jwt.secret), app.config.jwt.secret));
      if (user.length === 0) {
        return { msg: '账号不存在', code: 101 };
      } else if (user.length === 1 && user.state === 0) {
        return { msg: '账号已禁用', code: 102 };
      } else if (user.length === 1 && user[0].password !== ctx.helper.aesEncrypt(password, app.config.jwt.secret)) {
        return { msg: '密码不正确', code: 102 };
      } else if (user.length === 1 && user[0].password === ctx.helper.aesEncrypt(password, app.config.jwt.secret)) {
        return { msg: '登录成功', data: user[0], code: 0 };
      }
      return { msg: '未知错误', code: -1 };
    } catch (err) {
      ctx.logger.error('登录失败', err);
      return ctx.helper.body.ERROR({ ctx, msg: '未知错误' });
    }
  }

  async addUser(data) {
    const { ctx, app } = this;
    const { username, password } = data;
    const user = await this.app.mysql.query('select id from users where username = ?', username);
    ctx.logger.info('查询账户', user);
    if (user.length === 0) {
      const pwd = ctx.helper.aesEncrypt(password, app.config.jwt.secret);
      const rsp = await this.app.mysql.insert('users', { username, password: pwd, created_at: new Date() });
      ctx.logger.info('账号新增', rsp);
      if (rsp) {
        return ctx.helper.body.SUCCESS({ ctx, msg: '账号新增成功', data: null });
      }
      return ctx.helper.body.ERROR({ ctx, msg: '账号新增失败' });
    }
    return ctx.helper.body.ERROR({ ctx, msg: '账号已存在' });

  }

  async getUsers(data) {
    const { ctx } = this;
    const { pageNo, pageSize, username, startTime, endTime, email, gender, nickname, state, phone } = data;
    const res = await this.app.mysql.select('users', { // 搜索 users 表
      // where: [{ created_at: 'between 2021-11-06 12:00:00 and 2021-11-09 12:00:00' }], // WHERE 条件
      // columns: [ 'gender', 'phone' ], // 要查询的表字段
      orders: [[ 'created_at', 'desc' ], [ 'id', 'desc' ]], // 排序方式
      limit: pageSize, // 返回数据量
      offset: pageNo - 1, // 数据偏移量
    });
    if (res) {
      return ctx.helper.body.SUCCESS({ ctx, msg: '用户列表查询成功', res });
    }
    return ctx.helper.body.ERROR({ ctx, msg: '用户列表查询失败' });
  }

}


module.exports = UserService;
